<?php

declare(strict_types=1);

namespace app\common\model\Card;

use think\Model;
use app\common\model\Config;

/**
 * @mixin \think\Model
 */
class Card extends Model
{
    //
    protected $name = 'card';
    // 自动写入时间戳
    protected $autoWriteTimestamp = true;

    // 定义创建时间
    protected $createTime = 'createtime';

    // 定义更新时间
    protected $updateTime = false;
    protected $append = [
        'avatar_cdn',
    ];

    public function type()
    {
        return $this->belongsTo('app\common\model\Card\Type', 'typeid', 'id');
    }

    public function getAvatarCdnAttr($value, $data)
    {
        $avatar = is_file('./' . $data['avatar']) ? $data['avatar'] : '/static/home/res/images/avatar/default.png';

        $cdn = Config::where(['key' => 'cdn'])->value('value');

        return $cdn . $avatar;
    }
}
